import java.util.*;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Microsoft
 * Date: 2023-03-19
 * Time: 14:36
 */
class Student implements Comparable<Student>{
    public String name;
    public int age;
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}

class NameComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
class Incmp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);//大根堆
    }
}
public class Test {
    public static void main5(String[] args) {
        Incmp incmp = new Incmp();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(incmp);
        priorityQueue.offer(1);
        priorityQueue.offer(2);
    }




    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if (arr ==null || k == 0) {
            return ret;
        }
        Queue<Integer> minHeap = new PriorityQueue<>(k);//建立大小为k的小根堆

        //将arr当中的元素放入到小根堆当中
        for (int x:arr) {
            minHeap.offer(x);
        }


        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;
    }

    /**
     * 求前k个最大的元素
     * @param arr
     * @param k
     * @return
     */
    //总时间复杂度是O(N) = Nlog(k)
    public static int[] maxK(int[] arr, int k) {
        int[] ret = new int[k];
        if (arr ==null || k == 0) {
            return ret;
        }
        Queue<Integer> minHeap = new PriorityQueue<>(k);//建立大小为k的小根堆

        //将前k个元素放入到小根堆当中
        //时间复杂度K*log(k)
        for (int i = 0; i < k; i++) {
            minHeap.offer(arr[i]);
        }
        //从k+1个元素开始和堆顶元素进行比较，如果堆顶元素小的话，那么将堆顶元素出堆，将元素放入
        //时间复杂度(N-K)log(k)
        for (int i = k; i < arr.length; i++) {
            if (minHeap.peek() < arr[i]) {
                minHeap.poll();
                minHeap.offer(arr[i]);
            }
        }


        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;
    }
    public static void main3(String[] args) {
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("ahangsan",10);
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1, student2));
    }
    public static void main2(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(3);
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(list);
        priorityQueue.offer(10);
        priorityQueue.offer(1);

//        Queue<Student> priorityQueue1 = new PriorityQueue<>();
//        priorityQueue1.offer(new Student(10));
//        priorityQueue1.offer(null);
    }


    public static void main(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,36,65,49,25,34};
        testHeap.initElem(array);
        testHeap.createHeap();
        testHeap.heapSort();
    }
}










